import axios from 'axios'
import {notification} from 'antd';
import qs from 'qs'
const Axios = axios.create({
    timeout: 2000
})

Axios.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
            notification.open({
                message:'请求超时'
            });
            // return service.request(originalRequest);//例如再重复请求一次
        }
        // 需要重定向到错误页面
        const errorInfo = error.response;
        console.error('error info ==> ', errorInfo)
        if (errorInfo) {
            // error =errorInfo.data//页面那边catch的时候就能拿到详细的错误信息,看最下边的Promise.reject
            const errorStatus = errorInfo.status; // 404 403 500 ... 等
            notification.open({
                message:'请求超时'+errorStatus
            });
        }
        //可以判断对于request码
        return Promise.reject(error)
    }
)

Axios.interceptors.response.use( response => {
    let data;
    if (response.data === undefined) {
        data = response.request.responseText
    } else {
        data = response.data
    }
    return data
},error => {
    if(error && error.response) {
        //判断code码，给予对于错误提示
        return Promise.reject(error)
    }else {
        notification.open({
            message:'网络连接存在问题，请重试！'
        });
    }
})

export default {
    //表单方式提交
    post(url: string, data={}, isToken: boolean) {
        const token = window.sessionStorage.getItem('token');
        if(isToken) {
            return Axios({
                method: 'post',
                url: url,
                data: qs.stringify(data),
                timeout: 10000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                    'Authorization': token
                }
            })
        }else{
            return Axios({
                method: 'post',
                url: url,
                data: qs.stringify(data),
                timeout: 10000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                }
            })
        }
    },
    get(url: string, params = {}, isToken: boolean) {
        const token = window.sessionStorage.getItem('token');
        if (isToken) {
            return Axios({
                method: 'get',
                url: url,
                params,
                timeout: 20000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Authorization': token
                }
            })
        } else {
            return Axios({
                method: 'get',
                url: url,
                params,
                timeout: 20000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            })
        }
    },
    getDownload(url: string, params = {}, isToken: boolean) {
        const token = window.sessionStorage.getItem('token');
        let hasToken = Boolean(isToken && token)
        if (hasToken) {
            return Axios({
                method: 'get',
                url: url,
                params,
                timeout: 20000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest',
                    'Authorization': token
                },
                responseType: 'arraybuffer',
            })
        } else {
            return Axios({
                method: 'get',
                url: url,
                params,
                timeout: 20000,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                },
                responseType: 'arraybuffer',
            })
        }
    },
    postJson(url: string, data = {}, isToken: boolean) {
        const token = window.sessionStorage.getItem('token');
        let hasToken = Boolean(isToken && token)
        if (hasToken) {
            return Axios({
                method: 'post',
                url: url,
                data,
                timeout: 20000,
                headers: {
                    'Authorization': token
                }
            })
        } else {
            return Axios({
                method: 'post',
                url: url,
                data,
                timeout: 20000,
            })
        }
    },
}